/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.common;

import com.facebook.drawee.drawable.ScalingUtils;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Rect;

/** Custom scale type examples. */
public class CustomScaleTypes {

  public static final ScalingUtils.ScaleType FIT_X = new ScaleTypeFitX();
  public static final ScalingUtils.ScaleType FIT_Y = new ScaleTypeFitY();

  private static class ScaleTypeFitX extends ScalingUtils.AbstractScaleType {

    @Override
    public void getTransformImpl(
        Matrix outTransform,
        Rect parentRect,
        int childWidth,
        int childHeight,
        float focusX,
        float focusY,
        float scaleX,
        float scaleY) {
      float scale, dx, dy;
      scale = scaleX;
      dx = parentRect.left;
      dy = parentRect.top + (parentRect.getHeight() - childHeight * scale) * 0.5f;
      outTransform.setScale(scale, scale);
      outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
    }
  }

  private static class ScaleTypeFitY extends ScalingUtils.AbstractScaleType {

    @Override
    public void getTransformImpl(
        Matrix outTransform,
        Rect parentRect,
        int childWidth,
        int childHeight,
        float focusX,
        float focusY,
        float scaleX,
        float scaleY) {
      float scale, dx, dy;
      scale = scaleY;
      dx = parentRect.left + (parentRect.getWidth() - childWidth * scale) * 0.5f;
      dy = parentRect.top;
      outTransform.setScale(scale, scale);
      outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
    }
  }
}
